﻿//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ResourceManagement.Samples.ResourceManagementClient.ResourceManagement;

namespace Microsoft.ResourceManagement.Samples.ResourceManagementClient.Utilities
{
    public sealed class ResourceManagementAttributeFactory
    {
        private ResourceManagementAttributeFactory()
        {

        }
        /// <summary>
        /// Creates a new attribute object with a null default value
        /// </summary>
        /// <param name="attributeName"></param>
        /// <returns></returns>
        public static ResourceManagementAttribute CreateAttribute(String attributeName)
        {
            return CreateAttribute(attributeName, null);
        }
        /// <summary>
        /// Creates a new attribute object with the provided default value
        /// </summary>
        /// <param name="attributeName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static ResourceManagementAttribute CreateAttribute(String attributeName, Object value)
        {
            if(String.IsNullOrEmpty(attributeName))
            {
                throw new ArgumentNullException("attributeName", "The AttributeName cannot be null or empty");
            }

            bool isMultiValued = StaticSchemaManager.Instance.IsMultiValued(attributeName);

            ResourceManagementAttribute newAttrib = new ResourceManagementAttribute(isMultiValued, attributeName, value);
            return newAttrib;
        }

        public static ResourceManagementAttribute ConvertToMultiValuedAttribute(ResourceManagementAttribute attribute)
        {
            if (null == attribute)
            {
                throw new ArgumentNullException("attribute", "The attribute cannot be null");
            }
            if (attribute.IsMultiValued)
            {
                return attribute;
            }

            ResourceManagementAttribute newAttribute = new ResourceManagementAttribute(true, attribute.AttributeName);
            newAttribute.AddMultiValue(attribute.AttributeSingleValue);
            if (attribute.IsValueChanged == false)
                newAttribute.ClearIsChanged();
            return newAttribute;
        }
    }
}
